package com.xuzimian.globaldemo.thirdplatform.wechat.controller;


import com.xuzimian.globaldemo.thirdplatform.wechat.util.WechatNativeHelper;
import com.xuzimian.globaldemo.thirdplatform.wechat.util.WxJavaHelper;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.reactive.result.view.RedirectView;
import org.springframework.web.reactive.result.view.Rendering;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;

@Controller
public class HomeController {

    @Resource
    private WxJavaHelper wxJavaHelper;

    String baseUrl="https://www.baidu.com";

    @GetMapping("/nativeWxLogin")
    public Rendering  nativeLogin() throws UnsupportedEncodingException {
         return Rendering.redirectTo(WechatNativeHelper.getWechatLoginUrl("wxe23eff145240bcca", baseUrl)).build();
    }


    @GetMapping("/nativeWxAuthorize")
    public Rendering nativeWxAuthorize() throws UnsupportedEncodingException {
        String wxLoginUrl =WechatNativeHelper.getWechatOauth2AuthorizeUrl("wxe23eff145240bcca", baseUrl);
        return Rendering.redirectTo(wxLoginUrl).build();
    }

    @GetMapping("/wxJavaWxLogin")
    public Rendering wxJavaWxLogin() throws UnsupportedEncodingException {
        String wxLoginUrl = wxJavaHelper.getWxService().buildQrConnectUrl(baseUrl, "snsapi_login", "xzm");
        return Rendering.redirectTo(wxLoginUrl).build();
    }

    @GetMapping("/redirect1")
    public RedirectView redirect1() {
        return new RedirectView("/index", HttpStatus.FOUND);
    }

    @GetMapping("/redirect2")
    public Rendering redirect2() {
        return Rendering.redirectTo("/index").build();
    }

    @GetMapping("/redirect3")
    public RedirectView redirect3() {
        return new RedirectView("http://localhost:8081/index", HttpStatus.FOUND);
    }


}
